package com.lei.yohuo001java.fragmentyouhuo;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lei.mvp_retrofitlibrary.http.bean.GoodsBean;
import com.lei.mvp_retrofitlibrary.mvp.view.BaseFragment;
import com.lei.yohuo001java.R;
import com.lei.yohuo001java.adapter.MyGoodsAdapter2;
import com.lei.yohuo001java.adapter.MyStringAdapter;
import com.lei.yohuo001java.bean.StringBean;
import com.lei.yohuo001java.contract.ShopContract;
import com.lei.yohuo001java.model.ShopModel;
import com.lei.yohuo001java.presenter.ShopPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends BaseFragment<ShopPresenter> implements ShopContract.ShopView {
    private RecyclerView rv1;
    private List<StringBean> list = new ArrayList<>();
    private MyStringAdapter adapter;

    private RecyclerView frag01Rv1;
    private SmartRefreshLayout smart;
    private MyGoodsAdapter2 adapter2;
    private List<GoodsBean.DataBean> listdata = new ArrayList<>();
    private int page = 1;

    @Override
    public int bindlayout() {
        return R.layout.fragment_two;
    }

    @Override
    public void initView() {
        rv1 = (RecyclerView) findViewById(R.id.two_frag_rv1);
        frag01Rv1 = (RecyclerView) findViewById(R.id.two_frag_fl);
        smart = (SmartRefreshLayout) findViewById(R.id.smart_two);
        mPresenter = new ShopPresenter(new ShopModel(),this);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page",""+page);
        hashMap.put("pagesize","1000");
        mPresenter.initShop2(hashMap);
    }

    @Override
    public void initData() {

        list.add(new StringBean("男装"));
        list.add(new StringBean("品牌"));
        list.add(new StringBean("球鞋"));
        list.add(new StringBean("女装"));
        list.add(new StringBean("高街"));
        list.add(new StringBean("服配"));
        list.add(new StringBean("包类"));
        list.add(new StringBean("美妆"));
        list.add(new StringBean("生活"));
        list.add(new StringBean("数码"));
        list.add(new StringBean("潮玩"));
        list.add(new StringBean("潮童"));
        list.add(new StringBean("好物"));

        adapter = new MyStringAdapter(R.layout.string_layout,list);
        rv1.setAdapter(adapter);
        rv1.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.notifyDataSetChanged();
        list.get(0).setChecked(true);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                listdata.clear();
                //TODO
                page=position+1;
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("page",""+page);
                hashMap.put("pagesize","1000");
                mPresenter.initShop2(hashMap);
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setChecked(false);
                }
                list.get(position).setChecked(true);
                adapter.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                Toast.makeText(getContext(), page + "", Toast.LENGTH_SHORT).show();
            }
        });
        adapter2 = new MyGoodsAdapter2(R.layout.item2_layout,listdata);
        frag01Rv1.setAdapter(adapter2);
        frag01Rv1.setLayoutManager(new GridLayoutManager(getContext(),3));
        smart.setOnMultiPurposeListener(new OnMultiPurposeListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderReleased(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {

            }

            @Override
            public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterReleased(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterFinish(RefreshFooter footer, boolean success) {

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(500);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(500);
            }

            @Override
            public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

            }
        });
    }

    @Override
    public void initShop2(GoodsBean goodsBean) {
        if (goodsBean!=null){
            List<GoodsBean.DataBean> data = goodsBean.getData();
            listdata.addAll(data);
            adapter2.notifyDataSetChanged();
        }
    }

    @Override
    public void showToast(String msg) {

    }
}
